package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.domain.Question;
import com.itheima.dubbo.interfaces.QuestionApi;
import com.itheima.dubbo.mysql.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yaohu
 * @date 2021/12/12
 * @Version 1.0
 * @describe
 */
@Service
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 根据用户id查询她设置的问题
     *
     * @param userId
     * @return
     */
    @Override
    public Question findQuestionByUserId(Long userId) {
        return questionMapper.selectOne(Wrappers.lambdaQuery(Question.class).eq(Question::getUserId, userId));
    }

    /**
     * 保存陌生人问题
     *
     * @param question
     */
    @Override
    public void saveQuestion(Question question) {
        if (ObjectUtil.isNull(findQuestionByUserId(question.getUserId()))) {
            questionMapper.insert(question);
        } else {
            questionMapper.update(question, Wrappers.lambdaQuery(Question.class));
        }
    }
}
